<template>
  <view>
    <!-- 搜索栏 -->
    <my-search bgcolor="#ff00ff" radius="18" @click="searchClick"></my-search>
    <!-- 分类导航 -->
    <view class="cate-container">
      <!-- 左侧导航 -->
        <scroll-view class="cate-left" scroll-y="true" :style="{height: wh+'px'}">
          <view
            :class="['cate-left-item', i===activeIndex ? 'active' : '']"
            v-for="(item, i) in cateList"
            :key="i"
            @click="changeActive(i)"
          >
            {{item.cat_name}}
          </view>
        </scroll-view>
        
      <!-- 右侧导航 -->
      <scroll-view class="cate-right" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <!-- <view class="cate-right-list"> -->
          <view class="cate-right-item" v-for="(item, i) in cateLev2" :key="i">
            <!-- 二级分类标题 -->
            <view class="cate-right-title">
              /{{item.cat_name}}/
            </view>
            <!-- 二级分类内容 -->
            <view class="cate-right-box">
              <view class="cate-right-content" v-for="(item2, i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
                <image :src="item2.cat_icon" mode="widthFix"></image>
                <text>{{item2.cat_name}}</text>
              </view>
            </view>
          </view>
        <!-- </view> -->
      </scroll-view>
      
    </view>
  </view>
</template>

<script>
  // 导入mixin中的共同处理逻辑代码
  import tabBarBadge from '@/mixin/tabBar-index.js'
  
  export default {
    mixins: [tabBarBadge],
    data() {
      return {
        // 页面数据
        cateList: [], // 一级分类数据
        cateLev2: [], // 二级分类数据
        
        // 页面状态数据
        wh: 0, // 页面可用高度
        activeIndex: 0, // 一级分类的选中索引
        scrollTop: 0, // 二级分类滚动位置
      };
    },
    onLoad() {
      // 发起请求
      this.getCateList()
      // 获取屏幕可用高度
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight -50
      
    },
    methods:{
      // 获取分类数据
      async getCateList(){
        const { data: res } = await uni.$http.get('/categories')
        console.log(res)
        // 判断异常
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面数据
        this.cateList = res.message
        // 设置二级分类数据
        this.cateLev2 = this.cateList[0].children
        
      },
      changeActive(i){
        // 改变一级分类的选中状态
        this.activeIndex = i
        // 设置二级分类的数据
        this.cateLev2 = this.cateList[i].children
        // 设置二级分类滚动条位置
        this.scrollTop = Math.random()
      },
      // 跳转到商品列表
      gotoGoodsList(goods){
        console.log(goods)
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${goods.cat_id}`
        })
      },
      // mysearch组件的事件处理函数
      searchClick(e){
        console.log('=====searchClick', e)
        // 跳转到搜索页面
        uni.navigateTo({
          url: `/subpkg/search/search`
        })
      }
      
      
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .cate-container {
    display: flex;
    .cate-left{
      width: 220rpx;
      // height: 600rpx;
      .cate-left-item {
        width: 100%;
        height: 120rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #efefef;
        font-size: 28rpx;
        &.active {
          background-color: #ffffff;
          display: flex;
          justify-content: space-between;
          &::before{
            content: '';
            width: 4rpx;
            height: 60rpx;
            background-color: #c00000;
          }
          &::after{
            content: '';
            width: 4rpx;
            height: 60rpx;
            background-color: #ffffff;
          }
        }
      }
    }
    .cate-right{
      flex: 1;
      .cate-right-item {
        
        .cate-right-title {
          width: 100%;
          height: 80rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          font-weight: bold;
        }
        .cate-right-box {
          display: flex;
          flex-wrap: wrap;
          .cate-right-content {
            width: 33.33%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            box-sizing: border-box;
            padding: 16rpx;
            image {
              width: 100%;
            }
            text {
              font-size: 26rpx;
              // background-color: pink;
            }
          }
        }
      }
    }
  }

</style>
